package com.wjh.web;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.wjh.common.BaseInfo;
import com.wjh.common.Constants;
import com.wjh.util.RedisUtils;

import jakarta.servlet.http.HttpServletRequest;

import java.util.Enumeration;

/**
 * @author wenjianhai
 * @date 2023/6/21
 * @since JDK 17
 */
public class BaseController {
	@Autowired
	protected HttpServletRequest request;

	@Autowired
	protected RedisUtils jedisUtil;

	/**
	 * 得到request对象
	 */
	public HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		return request;
	}

	/**
	 * 得到ModelAndView
	 */
	public ModelAndView getModelAndView() {
		return new ModelAndView();
	}

	/**
	 * 获取基础信息
	 */
	public BaseInfo getBaseInfo() {
		Enumeration<String>  headers = request.getHeaderNames();
		String strtoken = request.getHeader("TOKEN");

		if (StringUtils.isBlank(strtoken)) {
			return null;
		}
		strtoken = Constants.LOGIN_TOKEN + strtoken;

		Object obj = jedisUtil.getObj(strtoken);

		return obj == null ? null : (BaseInfo) obj;
	}
}
